package cn.xc.org.controller;

import cn.xc.basic.util.AjaxResult;
import cn.xc.basic.util.ExcelUtils;
import cn.xc.basic.util.PageList;
import cn.xc.org.domain.Shop;
import cn.xc.org.domain.ShopAuditLog;
import cn.xc.org.domain.ShopDot;
import cn.xc.org.exception.BusinessException;
import cn.xc.org.query.ShopQuery;
import cn.xc.org.service.IShopService;
import com.sun.mail.imap.protocol.ID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "店铺api",description = "店铺CRUD")
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    /**
     * 查詢所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 查詢單個對象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Shop findOne(@PathVariable("id") Long id){
        return  shopService.findById(id);
    }

    /**
     * 分頁查詢
     * @param shopQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    /**
     * 刪除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "刪除")
    public AjaxResult del(@PathVariable("id") Long id){
        try{
            shopService.del(id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：批量刪除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try{
            shopService.patchDel(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：批量刪除
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "批量删除")
    public AjaxResult settlement(@RequestBody Shop shop){
        try{
            shopService.settlement(shop);
            return new AjaxResult();
        }catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统异常");
        }
    }

    /**
     * 接口：添加和修改
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加和修改")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try{
            if(shop.getId()==null){
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：店铺审核成功
     * @param shopAuditLog
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditPass(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    /**
     * 接口：店铺审核失败
     * @param shopAuditLog
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditReject(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    /**
     *  接口:數據導出
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtils.exportExcel(shops,null,"店鋪數據",Shop.class,"數據.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 接口:數據導入
     * @param file
     */
    @PostMapping("/importExcel")
    public void imortExcel(@RequestPart("file")MultipartFile file){
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    @GetMapping("/echarts")
    public List<ShopDot> echarts(){
       return shopService.echarts();
    }

    /**
     *  接口:激活成功
     * @param request
     */
    @GetMapping("/active")
    public String active(HttpServletRequest request){
        String ids = request.getParameter("id");
        Long aLong = Long.parseLong(ids);
        Shop shop = shopService.findById(aLong);
        shop.setState(3);
        shopService.update(shop);
        return "激活成功!";
    }

}
